<script>
import util from '@/common/util'
export default {
	onLaunch: function (e) {
		let pid = ''
		if (e.query) {
			if (e.query.pid) pid = e.query.pid
			if (e.query.scene) {
				pid = decodeURIComponent(e.query.scene);
			}
		}
		// this.getToKen()

		uni.setStorageSync('pid', pid);

		// #ifdef MP-WEIXIN
		uni.requirePrivacyAuthorize({
			success: () => { },
			fail: () => { }
		})
		uni.setStorageSync('platform', 'MP-WEIXIN');
		uni.setStorageSync('System','ios')
		this.login_bind();
		// uni.$on('restart_login', () => {
		// 	this.login_bind();
		// });
		// #endif

		// #ifdef APP-PLUS
		this.login_app()

		uni.onPushMessage(async (res) => {
			if (res.type === 'click' || res.type === 'receive') {
				let url = uni.getSystemInfoSync().platform == 'ios' ? JSON.parse(res.data.payload) :
					res.data.payload
				setTimeout(function () {
					uni.navigateTo({
						url,
					})
				}, 1000)
				plus.push.clear()
				plus.runtime.setBadgeNumber(0) //清除图标右上角数字
			}
		})

		// uni.$on('restart_login', () => {
		// 	this.login_app();
		// });

		uni.setStorageSync('platform', 'APP');
		//锁定 666 为APP进来的用户
		uni.setStorageSync('pid', 666);
		// #endif
	},
	async onShow() {
		// #ifdef APP-PLUS
		this.version_update()
		// #endif
	},
	onHide: function () {
		//用户下线
		this.$api.post('User/UserOffline');
	},
	methods: {
		
		// async getToKen(){
		// 	this.$nextTick(async ()=>{
		// 	let faceToken = await this.$util.sendPostRequest('https://skiapi.xiaojusmart.com/api/Common/GetAppToken',{
		// 			Auth_Key: '人脸识别' // 要发送的数据
		// 		})
		// 	let yellowToken = await this.$util.sendPostRequest('https://skiapi.xiaojusmart.com/api/Common/GetAppToken',{
		// 			Auth_Key: '图片鉴黄' // 要发送的数据
		// 		})
		// 	if(faceToken.data.code == 0){
		// 		let data = JSON.stringify(faceToken.data.data)
		// 		uni.setStorageSync("faceToken",data)
		// 	}
		// 	if(yellowToken.data.code == 0){
		// 		let data = JSON.stringify(yellowToken.data.data)
		// 		uni.setStorageSync("yellowToken",data)
		// 	}
		// 	})
		// },
		async login_bind() {
			let _this = this;
			uni.login({
				provider: 'weixin',
				success: res => {
					_this.wx_login(res.code);
				},
				fail: err => {
					// uni.showModal({
					// 	content: '登录失败',
					// 	showCancel: false
					// });
				}
			});
		},
		async login_app() {
			let _this = this;
			let token = uni.getStorageSync('xj_token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/start/isLogin'
				})
				return
			}

			setTimeout(async () => {
				let userID = uni.getStorageSync('user').ID;
				let res = await this.$api.post('User/GetUserInfo', { ID: userID });
				console.log(res,'appres');
				const user = res.data
				if (user) {
					uni.getLocation({
						type: 'gcj02',
						success: async function (r2) {
							let r = await _this.$api.post('/Member/SaveOnlineData', {
								Last_Lat: r2.latitude,
								Last_Lng: r2.longitude
							})
						},
					});
				}
				// const im = await this.$im.login(user.UserID, user.Name, user.HeadImgUrl)
				this.$store.commit('login', true);
				this.$store.commit('setUser', user);
				uni.setStorageSync('xj_token', d.Token);
				uni.setStorageSync('user', user);
			}, 500)
		},
		async wx_login(code) {
			uni.setStorageSync('loginKey', code)
			let c_model = '';
			let c_system = ''
			uni.getSystemInfo({
				success(res) {
					c_model = res.model;
					c_system = res.system;
					uni.setStorageSync('System',res.osName)
				}
			});
			let pid = uni.getStorageSync('pid');
			let res = await this.$api.post('User/WXLogin', {
				Code: code,
				pid: pid
			});

			uni.setStorageSync('xj_token', res.data.Token);

			let user = res.data.UserData;

			uni.setStorageSync('user', user);
			this.$store.commit('setUser', user);
			// const im = await this.$im.login(user.UserID, user.Name, user.HeadImgUrl)

			let _this = this;
			// uni.getLocation({
			// 	type: 'gcj02',
			// 	success: async function (r2) {
			// 		let addressInfo = await util.getAddress(r2);
			// 		let r = await _this.$api.post('/Member/SaveOnlineData', {
			// 			Last_Lat: r2.latitude,
			// 			Last_Lng: r2.longitude,
			// 			ClientModel: c_model,
			// 			ClientOS: c_system,
			// 			City: addressInfo?.result?.address_component?.city,
			// 			Province: addressInfo?.result?.address_component?.province
			// 		})
			// 	},
			// });
		},
		version_update() {
			const info = uni.getSystemInfoSync()
			uni.request({
				url: 'https://medical.xiaojusmart.com/api/version/update',
				data: {
					appid: info.appId,
					version: info.appVersion,
					wgtVer: info.appWgtVersion,
					platform: info.platform,
					deviceId: info.deviceId,
					brand: info.brand,
					model: info.model,
					system: info.system,
				},
				success: (res) => {
					res = res.data
					console.log('000000000000000====' + JSON.stringify(res));
					if (res.type == 2) return
					if (res.must == 2) {
						//后台默认运行更新
						this.update_version(res.url);
						return;
					}
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/update?data=' + JSON.stringify(res)
						});
					}, 3000)
				}
			});
		},
		update_version(url) {
			const _this = this
			uni.downloadFile({
				url,
				success(res) {
					if (res.statusCode == 200) {
						_this.installBtn(res.tempFilePath);
						return;
					}
				}
			});
		},
		installBtn(path) {
			plus.runtime.install(path, {
				force: false
			}, res => {
				//更新完重启app
				uni.showModal({
					content: '更新完成，是否确认重启？',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							plus.runtime.restart();
						}
					}
				});
			},
				err => {
					uni.$u.toast('更新失败')
				});
		}
	}
}
</script>

<style lang="scss">
@import "uview-ui/index.scss";
@import 'static/css/common.scss';
/*每个页面公共css */
</style>